package com.jvtd.demo.pickerView;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.jvtd.bean.ProvincesBean;
import com.jvtd.demo.R;
import com.jvtd.utils.ArrayUtils;
import com.jvtd.utils.DateUtils;
import com.jvtd.utils.ProvincesUtils;
import com.jvtd.widget.dialog.JvtdAlertDialog;
import com.jvtd.widget.dialog.JvtdLoadingDialog;
import com.jvtd.widget.pickerView.builder.JvtdOptionsPickerBuilder;
import com.jvtd.widget.pickerView.builder.JvtdProvincesPickerBuilder;
import com.jvtd.widget.pickerView.builder.JvtdSexsPickerBuilder;
import com.jvtd.widget.pickerView.builder.JvtdTimePickerBuilder;
import com.jvtd.widget.pickerView.view.JvtdOptionsPickerView;
import com.jvtd.widget.pickerView.view.JvtdProvincesPickerView;
import com.jvtd.widget.pickerView.view.JvtdSexsPickerView;
import com.jvtd.widget.pickerView.view.JvtdTimePickerView;
import com.jvtd.widget.toast.JvtdToast;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class PickerViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker_view);
        TextView btn1 = findViewById(R.id.bt1);
        TextView btn2 = findViewById(R.id.bt2);
        TextView btn3 = findViewById(R.id.bt3);
        TextView btn4 = findViewById(R.id.bt4);

        btn1.setOnClickListener(view -> showTimePicker());
        btn2.setOnClickListener(view -> showAddressPicker());
        btn3.setOnClickListener(view -> showSexsPicker());
        btn4.setOnClickListener(view -> showOtherPicker());
    }

    private void showOtherPicker() {
        List<String> strings = Arrays.asList("1", "2");
        List<List<String>> lists = Arrays.asList(Arrays.asList("1-1", "1-2"), Arrays.asList("2-1", "2-2", "2-3"));
        JvtdOptionsPickerView<String> jvtdOptionsPickerView = new JvtdOptionsPickerBuilder(this, (options1, options2, options3, v) -> showMessage(String.format("选择了 %s", lists.get(options1).get(options2)))).isCyclic(false).build();
        jvtdOptionsPickerView.show(strings, lists);
    }

    private void showSexsPicker() {
        JvtdSexsPickerView jvtdSexsPickerView = new JvtdSexsPickerBuilder(this, (options1, options2, options3, v) -> showMessage(ArrayUtils.SEXS.get(options1))).build();
        jvtdSexsPickerView.showSexs();
    }

    private void showAddressPicker() {
        showLoading();
        ProvincesUtils.getInstance(this).getProvincesData(new ProvincesUtils.OnObtainListener() {
            @Override
            public void onObtainSuccess(List<ProvincesBean> provincesBeans, List<List<ProvincesBean>> cityBeans, List<List<List<ProvincesBean>>> areaBeans) {
                initAddressPicker(provincesBeans, cityBeans, areaBeans);
                hideLoading();
            }

            @Override
            public void onObtainFailed() {
                hideLoading();
                showMessage("加载失败");
            }
        });

    }

    private void initAddressPicker(List<ProvincesBean> provincesBeans, List<List<ProvincesBean>> cityBeans, List<List<List<ProvincesBean>>> areaBeans) {
        JvtdProvincesPickerView jvtdProvincesPickerView = new JvtdProvincesPickerBuilder(this,
                (options1, options2, options3, v) ->
                        showMessage(String.format("%s-%s-%s", provincesBeans.get(options1).getName(), cityBeans.get(options1).get(options2).getName(),
                                areaBeans.get(options1).get(options2).get(options3).getName())
                        )).build();
        jvtdProvincesPickerView.showProvincesCityArea(provincesBeans, cityBeans, areaBeans);
    }

    private void showTimePicker() {
        JvtdTimePickerView timePickerView = new JvtdTimePickerBuilder(this,
                (date, v) ->
                        showMessage(DateUtils.long2StringYMDHMS(date.getTime())
                        )).isDialog(true).build();
        Dialog mDialog = timePickerView.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            timePickerView.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
//                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
        timePickerView.show();
    }

    private void showMessage(String s) {
        JvtdToast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    private void showLoading(){
        hideLoading();
        JvtdLoadingDialog.show(this);
    }

    private void hideLoading(){
        JvtdLoadingDialog.dismiss(this);
    }

}

